import {uploadUrl} from './config'
// ... 顶部导入保持不变 ...

interface UploadConfig<T = any> extends UniNamespace.UploadFileOption {
	success?: (res: UniNamespace.UploadFileSuccessCallbackResult) => void;
	fail?: (err: UniNamespace.GeneralCallbackResult) => void;
	formData?: Record<string, any>;
}

export function asyncUpload<T = any>(
	url: string,
	item: string,
	path: string
): Promise<T> {
	return new Promise<T>((resolve, reject) => {
		uni.uploadFile({
			url: uploadUrl + url,
			filePath: item,
			name: 'file',
			formData: {
				path: path
			},
			header: {
				'Content-Type': 'multipart/form-data',
				'x-source': 'dispatch',
				Authorization: `Bearer ${uni.getStorageSync('token')}`,
			},
			success: (res: UniNamespace.UploadFileSuccessCallbackResult) => {
				const fileData: T = JSON.parse(res.data);
				if ((fileData as any).code === 200) {
					return resolve(fileData);
				} else {
					uni.showToast({
						title: '上传失败!' + (fileData as any).code,
						icon: "error",
						duration: 2000
					}).then(r => {
						return reject(fileData);
					});
				}
			},
			fail: (err: UniNamespace.GeneralCallbackResult) => {
				uni.showToast({
					title: '上传失败' + err.errMsg,
					icon: "error",
					duration: 2000
				}).then(r => {
					return reject(err);
				});

			}
		} as UploadConfig<T>);
	});
}

